// wglfont.hpp
//

#ifndef _WGLFONT_HPP
#define _WGLFONT_HPP

#include "wgl/wglbase.hpp"

struct stbtt_fontinfo;

namespace wgl
{
  // GlyphImage
  //
  struct WGL_API GlyphImage
  {
    unsigned int metrics_loaded : 1{0};
    unsigned int rendered : 1{0};

    // metrics
    int advance;
    float s_advance;
    int lsb;
    int ix0;
    int iy0;
    int ix1;
    int iy1;
    
    // image
    int width;
    int height;
    size_t offset;
  };


  
  // Font
  //
  class WGL_API Font
  {
  public:
    Font ( std::string path,
           int size );
    ~Font ();

    int get_baseline () const { return baseline; }
    int get_descent () const { return descent; }
    float get_scaled_descent () const { return s_descent; }
    
    void get_text_size ( const std::string &text,
                         int &width,
                         int &height );
    void render_text_image ( const std::string &text,
                             float x,
                             float y );

  private:
    std::string path;
    int size;
    
    std::vector<char> font_buffer; // [FIXME] not sure i have to keep it
    stbtt_fontinfo *font_info{}; // stbtt_fontinfo

    float scale;
    int ascent;
    int descent;
    int line_gap;
    int baseline;

    float s_ascent;
    float s_descent;
    
    GlyphImage glyphs[256];
    std::vector<unsigned char> images{};

    void load_glyph_metrics ( GlyphImage &glyph,
                              int c );
    void render_glyph ( GlyphImage &glyph,
                        int c );
  };
}

#endif
